﻿using Brc_PaaS.Bll.Apollo;
using Brc_PaaS.Bll.System;
using Brc_PaaS.Common.Entity;
using Brc_PaaS.Core.Redis;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;

namespace Brc_PaaS.Api.Common
{
    /// <summary>
    /// 配置中心基础类
    /// </summary>
    [ApiController]
    public class ConfigBaseController : BaseController
    {
        /// <summary>
        /// 配置读取接口
        /// </summary>
        private readonly IApollo _apollo;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="configuration"></param>
        /// <param name="system"></param>
        /// <param name="apollo"></param>
        public ConfigBaseController(IConfiguration configuration, IApollo apollo, ISystem system) : base(configuration, system)
        {
            this._apollo = apollo;
        }
        private string systemId = string.Empty;
        private Ass_Apollo _apolloInfo = null;


        /// <summary>
        /// 当前访问的阿波罗基本信息
        /// </summary>
        protected Ass_Apollo ApolloInfo
        {
            get
            {
                if (_apolloInfo == null)
                {
                    if (!string.IsNullOrEmpty(SystemId))
                    {
                        using (IRedisManager redisManager = new RedisManager(RedisServerEntity))
                        {
                            var dbSystemInfo = redisManager.GetOrFetchFromHash<Ass_Apollo>($"Apollo:{SystemId}", id => { return _apollo.GetNamespaceBySystemId(SystemId).Data; });
                            if (dbSystemInfo.Status == 1)
                                _apolloInfo = dbSystemInfo;
                        }
                    }
                }
                return _apolloInfo;

            }
        }

    }
}